package com.ai.controller.user;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.aop.AuthorityAnnotation;
import com.ai.aop.FunctionType;
import com.ai.common.controller.BaseController;
import com.ai.common.exception.BusinessException;
import com.ai.common.model.Result;
import com.ai.common.model.vo.BackOperatorVO;
import com.ai.model.edu.cms.backoperator.Menu;
import com.ai.model.edu.cms.backoperator.vo.MenuVO;
import com.ai.service.cms.user.BackendOperatorService;
import com.ai.service.cms.user.MenuService;

/**
 * @author cyj
 *
 */
@Controller
public class MenuController extends BaseController {
    @Resource
    private MenuService menuService;

    @Resource
    private BackendOperatorService backendOperatorService;

    /**
     * 进入菜单管理
     * 
     * @return
     */
//    @AuthorityAnnotation(functionType = { FunctionType.UNICOM_BACKEND_MENU_MENUMANAGER_MENULIST })
    @RequestMapping("/menu/menuList")
    public String toMenuList(HttpServletRequest request) {
        return "user/menuList";
    }

    /**
     * 查找所有菜单
     * 
     * @return
     */
    @RequestMapping("/menu/selectAllMenu")
    @ResponseBody
    public List<MenuVO> selectAllMenu() {
        List<MenuVO> vos = menuService.selectAllMenu();
        return vos;
    }

    /**
     * 查找所有菜单
     * 
     * @return
     */
    @RequestMapping("/menu/selectAllActivityMenu")
    @ResponseBody
    public List<MenuVO> selectAllActivityMenu(HttpServletRequest request, Long backOperatorId) {
        BackOperatorVO backOperatorVO = this.validateIsLogin(request);
        if (!backOperatorVO.getId().equals(backOperatorId)) {
            BusinessException.throwMessage("未检测到登录信息，请先登录再试！");
        }
        List<MenuVO> vos = backendOperatorService.findMenuVOS(backOperatorId);
        return vos;
    }

    /**
     * 新增菜单
     * 
     * @return
     */
//    @AuthorityAnnotation(functionType = { FunctionType.UNICOM_BACKEND_MENU_MENUMANAGER_ADDMENU })
    @RequestMapping("/menu/addMenu")
    @ResponseBody
    public Result addMenu(@ModelAttribute Menu menu) {
        Result rs = new Result(true, Result.SUCCESS, "");
        menu.setCreateBy(1L);
        menu.setUpdateBy(1L);
        menuService.insert(menu);
        return rs;
    }

    /**
     * 更新菜单
     * 
     * @return
     */
//    @AuthorityAnnotation(functionType = { FunctionType.UNICOM_BACKEND_MENU_MENUMANAGER_UPDATEMENU })
    @RequestMapping("/menu/updateMenu")
    @ResponseBody
    public Result updateMenu(@ModelAttribute Menu menu) {
        Result rs = new Result(true, Result.SUCCESS, "");
        menuService.updateByPrimaryKeySelective(menu);
        return rs;
    }
}
